﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class Node_WaveLipSync : VisualScript_Node, IVisualScriptNode_NeedObject
    {
        public List<VisualScript_Node> Objects { get; set; }

        protected WaveReader WaveReader;

        private string wavefile;
        public string WaveFileName
        {
            get { return wavefile; }
            set
            {
                wavefile = value;
                WaveReader = new WaveReader(Restart, value, SoundEffectName, Engine.GameScreens[0]);
            }
        }

        public string SoundEffectName { get; set; }

        private bool restart;
        public bool Restart
        {
            get { return restart; }
            set
            {
                restart = value;
                if (WaveReader != null)
                    WaveReader.Repeat = restart;
            }
        }

        public bool Play
        {
            get { return false; }
            set
            {
                if (value && WaveReader != null)
                    WaveReader.Sound.Play();
            }
        }

        public Node_WaveLipSync(VisualScript vs)
            : base(vs)
        {
            this.RepresentationType = typeof(VSR_ObjectNeeded);
            Objects = new List<VisualScript_Node>();            
            Load();
        }

        public Node_WaveLipSync()
        {
            this.RepresentationType = typeof(VSR_ObjectNeeded);
            Load();
        }

        public override void StartRepresentation(VisualScriptEditor editor)
        {
            base.StartRepresentation(editor);
            List<string> descriptions = new List<string>();
            descriptions.Add("WaveFileName(*.wav)");
            descriptions.Add("Sound Effect");
            NodeInformation info = new NodeInformation(descriptions);
            editor.RequestInformation(this, info);
        }

        private void Load()
        {
            this.descriptionName = "WaveLip Sync";
        }

        public override void ReceiveInformation(NodeInformation Information)
        {
            base.ReceiveInformation(Information);
            SoundEffectName = Information.Answers[1];
            WaveFileName = Information.Answers[0];
        }

        public float lastSamp { get; protected set; }
        public float Samp { get; protected set; }        
        public override void Update()
        {
            base.Update();

            if (WaveReader != null)
            {
                WaveReader.Update();
                lastSamp = Samp;
                Samp = MathHelper.ToRadians(WaveReader.CurrentSample / 500);

                for (int i = 0; i < Objects.Count; i++)
                {
                    if (Objects[i] is IVisualScriptNode_Object)
                    {
                        IVisualScriptNode_Object obj = Objects[i] as IVisualScriptNode_Object;
                        if (obj.Object is sgMotionAnimatedActor)
                        {                            
                            sgMotionAnimatedActor actor = obj.Object as sgMotionAnimatedActor;

                            int UpperBone = actor.AnimationController.GetBoneId("MouthUp");
                            int LowerBone = actor.AnimationController.GetBoneId("MouthDown");
                            float final = AnimationHelper.AnimateStatic(
                               lastSamp, Samp, 5);
                            actor.AnimationController.SkinnedBoneCustomPositions[UpperBone] = new Vector3(final, 0, 0);
                            actor.AnimationController.SkinnedBoneCustomPositions[LowerBone] = new Vector3(-final, 0, 0);
                        }
                    }
                }
            }
        }
    }
}
